
/*
 * Name: dlladdr
 * Version: 0.1.0
 * Description: Simple utility to know the virtual addresses
 * of functions in DLLs.
 *
 * NOTE: This program does not use Balzebub and
 * only runs on Windows, as it is intended
 * as a temporary solution.
 * TO-DO: With DLL parsing support in Balzebub,
 * this program could be made cross-platform.
 */

#include <Windows.h>
#include <iostream>
#include <cstdlib>

constexpr int VERSION_MAJOR = 0;
constexpr int VERSION_MINOR = 1;
constexpr int VERSION_PATCH = 0;

void print_help() {
	std::cout << "Usage: dlladdr <dllname> <function>" << std::endl;
	exit(0);
}


void print_version() {
	std::cout << "dlladdr v";
	std::cout << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH << std::endl;
	std::cout << "This program is part of the Balzebub project." << std::endl;
	exit(0);
}


int main(int argc, char const *argv[]) {

	if(argc < 2) {
		print_help();
	}

	std::string arg1 = argv[1];
	if(arg1 == "-v" || arg1 == "--version") {
		print_version();
	} else if(arg1 == "-h" || arg1 == "--help") {
		print_help();
	}

	if(argc < 3) {
		print_help();
	}


	std::string dllname = argv[1];
	std::string function = argv[2];

	HMODULE lib = LoadLibraryEx(dllname.c_str(), nullptr, 0x00);
	if(!lib) {
		std::cout << "ERROR Cannot load library... Error code: " << GetLastError() << std::endl;
		exit(-1);
	}

	unsigned int address = (unsigned int) GetProcAddress(lib, function.c_str());
	if(address == 0x00) {
		std::cout << "ERROR Function " << function << "() is not contained in " << dllname << std::endl;
		exit(-2);
	}

	std::cout << "Function " << function << "() is located at address 0x";
	std::cout << std::hex << address << std::endl;

	return 0;
}
